package designpattern.struct.proxy.dynamic.cglibs;

import net.sf.cglib.proxy.Enhancer;

/**
 * CGlib工厂类
 *
 * @author: cuihao
 * @create: 2020-09-22 14:31
 **/
public class CGlibProxyFactory {
    public static Object getProxy(Class<?> clazz) {

        // 创建动态代理增强类
        Enhancer enhancer = new Enhancer();

        // 设置类加载器
        enhancer.setClassLoader(clazz.getClassLoader());

        // 设置被代理类
        enhancer.setSuperclass(clazz);

        // 设置方法拦截器
        enhancer.setCallback(new DynamicInterceptor());

        // 创建代理类
        return enhancer.create();
    }
}
